package com.sanpu.kxe.business.repay.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.business.repay.bean.RefundPlanOutput;
import com.sanpu.kxe.business.repay.contract.RepayConfig;
import com.sanpu.kxe.business.repay.interfaces.IRefundPlanListener;
import com.sanpu.kxe.utils.DateUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

/**
 * Description：还款计划
 * Created by 薛勇军 on 2019/4/10.
 */

public class RefundPlanAdapter extends BaseAdapter {

    private List<RefundPlanOutput.RefundPlan> datas = new ArrayList<>();

    private Context mContext;

    private boolean isOptionalMcc;

    private IRefundPlanListener mListener;

    public RefundPlanAdapter(List<RefundPlanOutput.RefundPlan> datas,
                             Context mContext, boolean isOptionalMcc, IRefundPlanListener mListener) {
        this.datas = datas;
        this.mContext = mContext;
        this.isOptionalMcc = isOptionalMcc;
        this.mListener = mListener;
    }

    public void setNotifyDataChanged(List<RefundPlanOutput.RefundPlan> data) {
        if (datas == null)
            datas = new ArrayList<>();
        datas.clear();
        if (data != null)
            datas.addAll(data);
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (datas == null)
            return 0;
        return datas.size();
    }

    @Override
    public RefundPlanOutput.RefundPlan getItem(int position) {
        if (datas != null && datas.size() > position)
            return datas.get(position);
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_refund_plan, null);
        }
        RelativeLayout headerView = ButterKnife.findById(convertView, R.id.headerView);
        TextView typeTv = ButterKnife.findById(convertView, R.id.typeTv);
        TextView dateTv = ButterKnife.findById(convertView, R.id.dateTv);
        LinearLayout businessLl = ButterKnife.findById(convertView, R.id.businessLl);
        TextView businessTv = ButterKnife.findById(convertView, R.id.businessTv);
        ImageView businessIv = ButterKnife.findById(convertView, R.id.businessIv);
        TextView moneyTv = ButterKnife.findById(convertView, R.id.moneyTv);
        TextView tradeFeeTv = ButterKnife.findById(convertView, R.id.tradeFeeTv);

        RefundPlanOutput.RefundPlan refundPlan = getItem(position);
        if (refundPlan != null) {
            if (TextUtils.equals(RepayConfig.TRADETYPE_EXPENSE, refundPlan.getRepayTradeType())) {
                typeTv.setText("消费");
                headerView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.c_de2149));
            } else if (TextUtils.equals(RepayConfig.TRADETYPE_PAYMENT, refundPlan.getRepayTradeType())) {
                typeTv.setText("还款");
                headerView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.c_2189de));
            }
            String tradeTime = refundPlan.getExpectedTradeTime();
            if (!TextUtils.isEmpty(tradeTime) && tradeTime.length() > 4) {
                dateTv.setText(DateUtils.dateToString(tradeTime, "yyyy-MM-dd HH:mm:ss"));
            }
            if (isOptionalMcc) {
                if (TextUtils.equals(RepayConfig.TRADETYPE_EXPENSE, refundPlan.getRepayTradeType())) {
                    businessIv.setVisibility(View.VISIBLE);
                    businessTv.setText(refundPlan.getMccName());
                    businessLl.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mListener.checkBusiness(position);
                        }
                    });
                } else {
                    businessIv.setVisibility(View.GONE);
                    businessTv.setText("-----");
                }
                tradeFeeTv.setVisibility(View.GONE);
            } else {
                businessLl.setVisibility(View.GONE);
                tradeFeeTv.setVisibility(View.VISIBLE);
            }
            if (!TextUtils.isEmpty(refundPlan.getTradeAmount())) {
                moneyTv.setText(refundPlan.getTradeAmount());
            }
            if (!TextUtils.isEmpty(refundPlan.getTradeFee())) {
                tradeFeeTv.setText(refundPlan.getTradeFee());
            }
        }
        return convertView;
    }
}
